package mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import mybatisplus.config.Resp;
import mybatisplus.entity.Jiaban;
import mybatisplus.service.JiabanService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Date;
import java.util.List;

import static java.time.temporal.ChronoField.HOUR_OF_DAY;

/**
 * @Auther: tomj
 * @Date: 2019-03-12 10:26
 * @Description:
 */
@RestController
public class JiabanController {

    @Resource
    private JiabanService jiabanService;

    @RequestMapping("/admin/jiaban/insert")
    public Resp insert(String beizhu){
        Jiaban jiaban = new Jiaban();
        jiaban.setComments(beizhu);
        jiaban.setStartTime("19:00");
        String endTime = LocalDateTime.now().format(new DateTimeFormatterBuilder()
                .appendValue(HOUR_OF_DAY, 2)
                .appendLiteral(":00")
                .toFormatter());
        jiaban.setEndTime(endTime);
        Date date = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        jiaban.setRiqi(date);
        jiaban.setCreateTime(date);
        jiaban.setUpdateTime(date);
        if(jiabanService.save(jiaban)){
            return Resp.getSuccessResp("","加班成功");
        }else{
            return Resp.getFaildResp("添加打ka失败！检查服务器！");
        }

    }

    @RequestMapping("/admin/jiaban/list")
    public Resp getJiabanList(@RequestParam String riqi){
        try {
            QueryWrapper<Jiaban> wrapper = new QueryWrapper<Jiaban>();
            //1
            int dayOfYear = LocalDate.now().getYear();
            String start = dayOfYear + "-" + riqi + "-01";
            String end = dayOfYear + "-" + riqi + "-30";
            wrapper.gt("riqi",start).lt("riqi",end);
            List<Jiaban> list = jiabanService.list(wrapper);
            return Resp.getSuccessResp(list,"加班记录加载成功！");
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;

    }
}
